package jsyf.oa.service.email;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.email.DraftsDao;
import jsyf.oa.dao.email.EmailDao;
import jsyf.oa.dao.email.InboxDao;
import jsyf.oa.entity.email.EmailEntity;
import jsyf.oa.entity.email.EmailMainEntity;
import jsyf.oa.facade.email.DraftFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by y on 2018/3/7.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class DraftService implements DraftFacade{
    @Autowired
    private DraftsDao draftsDao;
    @Autowired
    private EmailDao emailDao;
    @Override
    public JSONObject findDraftList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId=jsonParams.getString("userId");
        String requestPage;
        String pageSize;
        StringBuilder condition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(userId)){
            condition.append(" and CREATE_USER_ID=? and DRAFT=1");
            sqlArgs.add(userId);
        }
        if (jsonParams.containsKey("requestPage") && jsonParams.containsKey("pageSize")) {
            requestPage = jsonParams.getString("requestPage");
            pageSize = jsonParams.getString("pageSize");
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        List<JSONObject> records=emailDao.getJSONListByPage(condition.toString(),sqlArgs.toArray(),Integer.valueOf(requestPage),Integer.valueOf(pageSize));
        int recordCount = emailDao.getPageCount(condition.toString(), sqlArgs.toArray());
        for (int i = 0; i < records.size(); i++) {
            JSONObject entity=records.get(i);
            //查询这条邮件的收件人
            List<JSONObject> receivers=draftsDao.getByEmailReceiver(entity.getString("id"),"0");
            //查询这条邮件的抄送人
            List<JSONObject> copiers=draftsDao.getByEmailReceiver(entity.getString("id"),"1");
            entity.put("receivers",receivers);
            entity.put("copiers",copiers);
        }
        int recordSize = Integer.valueOf(pageSize).intValue();

        int pageCount = 0;
        if (recordCount < recordSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / recordSize);
            pageCount = ((int) flag) + 1;
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        JSONObject result = new JSONObject();
        JSONObject objSendData = new JSONObject();
        //子对象
        objSendData.put("recordCount", recordCount);
        objSendData.put("pageCount", pageCount);
        objSendData.put("requestPage", 1);
        result.put("pager", objSendData);
        // 子数组对象
        result.put("records", records);
        return result;

    }

    @Override
    public JSONObject findDraftDetail(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String emailId=jsonParams.getString("id");
        JSONObject jsonObject=draftsDao.getJSONById(emailId);
        //查询这条邮件的收件人
        List<JSONObject> receivers=draftsDao.getByEmailReceiver(emailId,"0");
        //查询这条邮件的抄送人
        List<JSONObject> copiers=draftsDao.getByEmailReceiver(emailId,"1");
        //查询附件
        List<JSONObject> attaches=draftsDao.findByAttaches(emailId);
        jsonObject.put("receivers",receivers);
        jsonObject.put("copiers",copiers);
        jsonObject.put("attaches",attaches);
        return jsonObject;
        //return result;
    }



    @Override
    public Boolean removeById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        return draftsDao.removeByCondition(" AND ID = ? ",new String[]{jsonParams.getString("id")})>0;
    }
}
